package com.link2room.rodimus.controller.api;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.common.dto.threadlocal.ApiThreadLocal;
import com.link2room.common.exception.SysException;
import com.link2room.common.util.L2RAssert;
import com.link2room.rodimus.dto.param.api.SignInCalendarApiParamDto;
import com.link2room.rodimus.dto.param.api.SignInDrawAwardParamDto;
import com.link2room.rodimus.dto.param.api.SignInReissueParamDto;
import com.link2room.rodimus.dto.param.base.*;
import com.link2room.rodimus.facade.ISignInApiFacadeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.SIGN_IN_ERROR;

/**
 * Created by LN on 2018/1/2
 */
@Api(tags = "签到相关")
@RestController
@RequestMapping("/api/SignIn")
public class SignInApiController {

    @Resource
    private ISignInApiFacadeService signInApiFacadeService;

    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "签到", response = BaseResultDto.class)
    @ApiImplicitParams(value = {
    })
    public BaseResultDto doSign() throws Exception {
        signInApiFacadeService.doSignNowDay();
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/isSign", method = RequestMethod.GET)
    @ApiOperation(value = "是否签到")
    @ApiImplicitParams(value = {
    })
    public BaseResultDto isSign() throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(signInApiFacadeService.isSign());
        return resDto;
    }

    @RequestMapping(path = "/Reissue", method = RequestMethod.POST)
    @ApiOperation(value = "补签")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "paramDto", value = "paramDto", required = true, paramType = "body"),
    })
    public BaseResultDto reissue(@RequestBody  SignInReissueParamDto paramDto) throws Exception {
        signInApiFacadeService.reissue(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/Calender", method = RequestMethod.GET)
    @ApiOperation(value = "签到日历")
    @ApiImplicitParams(value = {

    })
    public BaseResultDto<SignInCalendarDto> signInCalendar(
            @RequestParam(name = "month") String month
    ) throws Exception {
        SignInCalendarApiParamDto paramDto = new SignInCalendarApiParamDto();
        paramDto.setMonth(month);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(signInApiFacadeService.signInCalendar(paramDto));
        return resDto;
    }

    @RequestMapping(path = "/currentAwards", method = RequestMethod.GET)
    @ApiOperation(value = "当前奖励周期列表", response = SignInAwardDto.class)
    @ApiImplicitParams(value = {

    })
    public BaseResultDto<SignInAwardDto> currentAwards(
    ) throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(signInApiFacadeService.currentAwards(new BaseParamDto()));
        return resDto;
    }


    @RequestMapping(path = "/currentEveryDayAwards", method = RequestMethod.GET)
    @ApiOperation(value = "当前每日奖励", response = SignInAwardDto.class)
    @ApiImplicitParams(value = {

    })
    public BaseResultDto<SignInAwardDto> currentEveryDayAwards(
    ) throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(signInApiFacadeService.currentEveryDayAwards(new BaseParamDto()));
        return resDto;
    }

    @RequestMapping(path = "/currentAwards/drawAward", method = RequestMethod.POST)
    @ApiOperation(value = "领取奖励")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "awardParamDto", value = "awardParamDto", required = true, paramType = "body"),
    })
    public BaseResultDto drawAward(
            @RequestBody SignInDrawAwardParamDto awardParamDto
    ) throws Exception {
        signInApiFacadeService.drawAward(awardParamDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/currentUserReissueTimes", method = RequestMethod.GET)
    @ApiOperation(value = "补签次数")
    @ApiImplicitParams(value = {

    })
    public BaseResultDto<SignInCurrentUserReissueTimesDto> currentUserReissueTimes(
    ) throws Exception {

        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultInfo(signInApiFacadeService.signInCurrentUserReissueTimes());
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/currentActivityInfo", method = RequestMethod.GET)
    @ApiOperation(value = "当前活动详情", response = SignInActiviyInfoDto.class)
    @ApiImplicitParams(value = {
    })
    public BaseResultDto<SignInActiviyInfoDto> currentActivityInfo(
    ) throws Exception {

        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultInfo(signInApiFacadeService.currentActivityInfo(new BaseL2rParamDto()));
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/currentUserShareDetails", method = RequestMethod.GET)
    @ApiOperation(value = "当前用户助力详情", response = SignInReissueDetailShareDto.class)
    @ApiImplicitParams(value = {

    })
    public BaseResultDto<SignInReissueDetailShareDto> sharePageInfo(
    ) throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        resDto.setQueryResult(signInApiFacadeService.currentUserShareDetails());
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/isHelper", method = RequestMethod.GET)
    @ApiOperation(value = "是否助力者", response = SignInReissueDetailShareDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "from", value = "from", required = true, paramType = "query"),
    })
    public BaseResultDto isHelper(
            @RequestParam(name = "from") String from
    ) throws Exception {
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        L2RAssert.isTrue(null == memberSession, new SysException(SIGN_IN_ERROR, "请登录."));
        L2RAssert.isTrue(DefaultValue.F.equals(memberSession.getIsMember()), new SysException(SIGN_IN_ERROR, "请先注册会员."));
        String memberNo = memberSession.getMemberNo();
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultInfo(memberNo.equals(from)?DefaultValue.F : DefaultValue.T);
        resDto.setResultCode(0);
        return resDto;
    }


}
